//
//  Utils.m
//  CarOnlineConsult
//
//  Created by 黄卫 on 17/2/24.
//  Copyright © 2017年 huangwei. All rights reserved.
//

#import "Utils.h"
#import <CommonCrypto/CommonDigest.h>

@implementation Utils

/**
 *  md5加密
 *
 *  @param str 加密前的字符串
 *
 *  @return 加密后的字符串
 */
+ (NSString *)md5:(NSString *)str
{
    const char *cStr = [str UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}



+(BOOL)CheckPhoneNumInput:(NSString *)_text{
    NSString *Regex =@"^[1][3-8]+\\d{9}";
    NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", Regex];
    return [mobileTest evaluateWithObject:_text];
}

#pragma 正则匹配用户密码6-20位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password
{
    NSString *pattern = @"^[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}

#pragma 正则匹配内容的有效性
+ (BOOL)VerityContentWithString:(NSString *)content
{
    // 将仅含有字母数字、符号的输入过滤掉
    NSString *string = @"[\u3000-\u301e\ufe10-\ufe19\ufe30-\ufe44\ufe50-\ufe6b\uff01-\uffee]{1,}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", string];
    BOOL isMatch = [pred evaluateWithObject:content];
    return isMatch;
}

#pragma mark 数字
+(BOOL)CheckNumber:(NSString *)number
{
    NSString *str = @"^([+-]?)\\d*\\.?\\d+$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:number];
}

#pragma mark 邮箱
+(BOOL)CheckEmail:(NSString *)email
{
    NSString *str = @"^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:email];
}

#pragma mark 中文字符
+(BOOL)CheckChinese :(NSString *)chinese
{
    NSString *str = @"^[\u4E00-\u9FA5\uF900-\uFA2D]+$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:chinese];
}

#pragma mark 邮编
+(BOOL)CheckPost:(NSString *)post
{
    NSString *str = @"^\\d{6}$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:post];
}

#pragma mark IP 地址
+(BOOL)CheckIP:(NSString *)ip
{
    NSString *str = @"^(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)\\.(25[0-5]|2[0-4]\\d|[0-1]\\d{2}|[1-9]?\\d)$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:ip];
}

#pragma mark 日期格式
+(BOOL)CheckDate:(NSString *)date
{
    NSString *str = @"^\\d{4}(\\-|\\/|\\.)\\d{1,2}\\1\\d{1,2}$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:date];
}

#pragma mark QQ号码
+(BOOL)CheckQQ:(NSString *)qq
{
    NSString *str = @"^[1-9]*[1-9][0-9]*$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:qq];
}

#pragma mark 电话号码
+(BOOL)CheckTelephone:(NSString *)telephone
{
    NSString *str = @"^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:telephone];
}

#pragma mark 手机号
+(BOOL)CheckPhone:(NSString *)phone
{
    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|7[01678]|8[0-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:phone];
}

#pragma mark 字母
+(BOOL)CheckLetter:(NSString *)letter
{
    NSString *str = @"^[A-Za-z]+$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:letter];
}

#pragma mark 身份证号码
+(BOOL)CheckIdCard:(NSString *)idCard
{
    NSString *str = @"^[1-9]([0-9]{14}|[0-9]{17})$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:idCard];
}

#pragma mark URL
+(BOOL)CheckUrl:(NSString *)url
{
    NSString *str = @"^((https|http|ftp|rtsp|mms)?://)?(([0-9a-z_!~*'().&=+$%-]+:)?[0-9a-z_!~*'().&=+$%-]+@)?(([0-9]{1,3}\\.){3}[0-9]{1,3}|([0-9a-z_!~*'()-]+\\.)*([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\.[a-z]{2,6})(:[0-9]{1,4})?((/?)|(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";
    NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
    return [regex evaluateWithObject:url];
}

#pragma mark 是否包含中文
+(BOOL)CheckContainChinese:(NSString *)chinese
{
    for(int i=0; i< [chinese length];i++) {
        int a = [chinese characterAtIndex:i];
        if( a > 0x4E00 && a < 0x9FFF) {
            return YES;
        }
    }
    return NO;
}

#pragma mark - 根据颜色得到
+(UIImage *)createImageWithColor:(UIColor *)color imageSize:(CGSize)size
{
    CGSize imageSize = CGSizeMake(size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(imageSize, 0, [UIScreen mainScreen].scale);
    [color set];
    UIRectFill(CGRectMake(0, 0, imageSize.width, imageSize.height));
    UIImage *pressedColorImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return pressedColorImg;
}


static NSBundle *bundle = nil;

+ ( NSBundle * )bundle{
    return bundle;
}

+(NSString *)userLanguage{
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *language = [def valueForKey:@"userLanguage"];
    return language;
}

+(void)setUserlanguage:(NSString *)language{
    
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    
    //1.第一步改变bundle的值
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj" ];
    bundle = [NSBundle bundleWithPath:path];
    
    //2.持久化
    [def setValue:language forKey:@"userLanguage"];
    [def synchronize];
}

/**
 *  获取应用版本号
 */
+ (NSString*)currentVersion
{
    NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
    NSString *version = [info objectForKey:@"CFBundleShortVersionString"];
    if (!version) {
        version = @"IOS_X";
    }
    return version;
}

/**
 *  字典转json
 */
+ (NSString *)dictionaryToJson:(NSDictionary *)dic
{
    NSError *parseError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
    //    NSString *json = [Utils dictionaryToJson:returnValue];
    //    NSLog(@"字典转json===%@",json);
}


/**
 *  格式化价格,是小数就保留两位,不是小数就取整数
 */
+ (NSString *)formatPriceValue:(CGFloat)originValue
{
    //取浮点型的整数位的价格
    CGFloat intValue = floor(originValue);
    NSString *formatValue = nil;
    if ((originValue-intValue) > 0) {//如果不是整数就显示两位
        formatValue = [NSString stringWithFormat:@"%.2f",originValue];
    } else {
        formatValue = [NSString stringWithFormat:@"%.0f",originValue];
    }
    return formatValue;
}

/**
 *  产生随机颜色
 */
+ (UIColor *)randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

@end
